package net.sf.fc.io.event;

import static net.sf.fc.io.event.FileCopyEvent.Level;

import java.io.File;
import java.io.PrintStream;

public final class ConsoleFileCopyEventListener implements
        FileCopyEventListener {

    private final Level lvl;

    public ConsoleFileCopyEventListener(Level lvl) {
        this.lvl = lvl;
    }

    @Override
    public void statusChanged(FileCopyEvent event) {

        // Only process if the level is equal to or higher than the one for the event.
        if(event.getLevel().compareTo(lvl) >= 0) {

            switch(event.getCopyStatus()) {
            case COPY_IN_PROGRESS:
                // TODO: Implement this.
                break;

            case COPY_COMPLETE:
            case COPY_FAILED:
            case RENAME_COMPLETE:
            case RENAME_FAILED:
                logStatus(event);
                break;

            case COPY_SKIP_DST_EXISTS:
            case COPY_SKIP_UPDATE_ONLY:
                logSkip(event);
                break;

            default:
               break;
            }
        }
    }

    private void logSkip(FileCopyEvent event) {

        PrintStream printStream = Level.WARN.compareTo(event.getLevel()) >= 0 ? System.out : System.err;

        File srcFile = event.getSrcFile();
        File dstFile = event.getDstFile();

        if(srcFile != null && dstFile != null) {
            StringBuilder logMessage = new StringBuilder();
            logMessage.append("File not copied from '").append(srcFile).append("' to '").append(dstFile).append("'");
            switch(event.getCopyStatus()) {
            case COPY_SKIP_DST_EXISTS:
                logMessage.append(" because destination file exists");
                break;

            case COPY_SKIP_UPDATE_ONLY:
                logMessage.append(" because destination file is newer than source file");
                break;

            default:
                break;
            }

            printStream.println(logMessage.toString());
        }

        Throwable t = event.getException();
        if(t != null) {
            t.printStackTrace(printStream);
        }
    }

    private void logStatus(FileCopyEvent event) {

        PrintStream printStream = Level.WARN.compareTo(event.getLevel()) >= 0 ? System.out : System.err;

        File srcFile = event.getSrcFile();
        File dstFile = event.getDstFile();

        if(srcFile != null && dstFile != null) {
            StringBuilder logMessage = new StringBuilder();

            switch(event.getCopyStatus()) {
            case COPY_COMPLETE:
                logMessage.append("File copied from '");
                break;

            case COPY_FAILED:
                logMessage.append("File not copied from '");
                break;

            case RENAME_COMPLETE:
                logMessage.append("File renamed from '");
                break;

            case RENAME_FAILED:
                logMessage.append("File not renamed from '");
                break;
            }

            logMessage.append(srcFile).append("' to '").append(dstFile).append("'");
            printStream.println(logMessage.toString());
        }

        Throwable t = event.getException();
        if(t != null) {
            t.printStackTrace(printStream);
        }
    }

}
